package com.se.dao.mysql;

import cn.hutool.json.JSONUtil;
import com.se.domain.dto.QuestionDTO;
import com.se.domain.mysql.OptionDO;
import com.se.domain.mysql.QuestionDO;
import com.se.domain.mysql.QuestionnaireDO;
import com.se.domain.request.param.QParam;
import com.se.domain.vo.OptionVO;
import com.se.domain.vo.QVO;
import com.se.mapper.OptionMapper;
import com.se.mapper.QuestionMapper;
import com.se.mapper.QuestionnaireMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Repository
public class QuestionDao {
    @Autowired
    QuestionnaireMapper questionnaireMapper;
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    OptionMapper optionMapper;


    public QuestionDO getByQId(Long qId){
        if(qId == null){
            return null;
        }
        return questionMapper.getByQId(qId);
    }

    @Transactional
    public Long createQn(Long actId, String qnJson) {
        List<QParam> qParamList = JSONUtil.toList(qnJson, QParam.class);
        //由于其他接口的设计问题尽管活动没有问卷也要添加qnId，
//        if(qParamList.isEmpty()){
//            return null;
//        }

        QuestionnaireDO questionnaireDO = new QuestionnaireDO(actId);
        questionnaireMapper.insert(questionnaireDO);

        if(qParamList.isEmpty()){
            return questionnaireDO.getQnId();
        }
        List<QuestionDTO> questionDTOList = new ArrayList<>();

        for (QParam qParam : qParamList) {
            QuestionDTO questionDTO = new QuestionDTO(questionnaireDO.getQnId(), qParam);
            questionDTOList.add(questionDTO);
        }

        questionMapper.insertAllDTO(questionDTOList);

        List<OptionDO> optionDOList = new ArrayList<>();
        for (QuestionDTO questionDTO : questionDTOList) {
            for (OptionDO optionDO : questionDTO.getOptionDOList()) {
                optionDO.setQId(questionDTO.getQId());
            }
            optionDOList.addAll(questionDTO.getOptionDOList());
        }
        if(!optionDOList.isEmpty()){
            optionMapper.insertAll(optionDOList);
        }

        return questionnaireDO.getQnId();
    }

    public List<QVO> listQVOByQnId(Long qnId) {
        if(qnId == null){
            return Collections.emptyList();
        }
        List<QuestionDO> questionDOList = questionMapper.listByQnId(qnId);
        if(questionDOList.isEmpty()){
            return Collections.emptyList();
        }
        Map<Long, QVO> voMap = new HashMap<>();
        List<QVO> qvoList = new ArrayList<>();
        List<Long> qIdList = new ArrayList<>();
        for (QuestionDO questionDO : questionDOList) {
            qIdList.add(questionDO.getQId());

            QVO qvo = new QVO(questionDO);
            qvoList.add(qvo);
            voMap.put(questionDO.getQId(), qvo);
        }

        List<OptionDO> optionDOList = optionMapper.listByQIds(qIdList);
        for (OptionDO optionDO : optionDOList) {
            QVO qvo = voMap.get(optionDO.getQId());
            qvo.getOptions().add(new OptionVO(optionDO));
        }

        return qvoList;
    }

    public List<OptionVO> listOVOByQId(Long qId) {
        if(qId == null){
            return null;
        }

        List<OptionDO> optionDOList = optionMapper.listByQId(qId);
        if(optionDOList.isEmpty()){
            return Collections.emptyList();
        }

        List<OptionVO> voList = new LinkedList<>();

        for (OptionDO optionDO : optionDOList) {
            voList.add(new OptionVO(optionDO));
        }

        return voList;
    }
}
